enum BTN {
    //% block="A"
    a,
    //% block="B"
    b
}


//% color="#ef784c" iconWidth=50 iconHeight=40
namespace Unihiker_camera_sample{

    //% block="初始化摄像头直到成功,编号[CAMNUM],屏幕宽[WIDTH]高[HEIGHT]" blockType="command"
    //% CAMNUM.shadow="number" CAMNUM.defl="0"
    //% WIDTH.shadow="number" WIDTH.defl="240"
    //% HEIGHT.shadow="number" HEIGHT.defl="320"
    export function readcap(parameter: any, block: any) {
        let num=parameter.CAMNUM.code;
        let width=parameter.WIDTH.code;
        let height=parameter.HEIGHT.code;
        Generator.addImport(`import cv2\nimport numpy as np`)
        Generator.addCode(`WIDTH = ${width}`)
        Generator.addCode(`HEIGHT = ${height}`)
        Generator.addCode(`cap = cv2.VideoCapture(${num})`)
        Generator.addCode(`cap.set(cv2.CAP_PROP_FRAME_WIDTH, WIDTH)`)
        Generator.addCode(`cap.set(cv2.CAP_PROP_FRAME_HEIGHT, HEIGHT)`)
        Generator.addCode(`cap.set(cv2.CAP_PROP_BUFFERSIZE, 1)`)
        Generator.addCode(`cv2.namedWindow('cvwindow',cv2.WND_PROP_FULLSCREEN)`)
        Generator.addCode(`cv2.setWindowProperty('cvwindow', cv2.WND_PROP_FULLSCREEN, cv2.WINDOW_FULLSCREEN)`)
        Generator.addCode(`pic_count = 0`)             
    Generator.addCode(`while not cap.isOpened():
    continue`)    
    }


    //% block="拍照并保存为[NAME]" blockType="command"
    //% NAME.shadow="string" NAME.defl="img.png"
    export function savecapimage(parameter: any, block: any) {
        let name=parameter.NAME.code;
        Generator.addCode(`cv2.waitKey(2)
cvimg_success, img_src = cap.read()
cvimg_h, cvimg_w, cvimg_c = img_src.shape
cvimg_w1 = cvimg_h*WIDTH//HEIGHT
cvimg_x1 = (cvimg_w-cvimg_w1)//2
img_src = img_src[:, cvimg_x1:cvimg_x1+cvimg_w1]
img_src = cv2.resize(img_src, (WIDTH, HEIGHT))
cvimg_src=img_src.copy()
print(${name})
cv2.imwrite("./"+${name}, cvimg_src)
`)
    }

    //% block="摄像(需在循环中执行)" blockType="command"
    export function readcapcapture(parameter: any, block: any) {
        Generator.addCode(`cv2.waitKey(5)
cvimg_success, img_src = cap.read()
cvimg_h, cvimg_w, cvimg_c = img_src.shape
cvimg_w1 = cvimg_h*WIDTH//HEIGHT
cvimg_x1 = (cvimg_w-cvimg_w1)//2
img_src = img_src[:, cvimg_x1:cvimg_x1+cvimg_w1]
img_src = cv2.resize(img_src, (WIDTH, HEIGHT))
cvimg_src=img_src.copy()
cv2.imshow('cvwindow', cvimg_src)
`)
    }

    //% block="摄像中拍照并保存为[NAME](需在循环中执行)" blockType="command"
    //% NAME.shadow="string" NAME.defl="img.png"
    export function takephotoandsave(parameter: any, block: any) {
        let name=parameter.NAME.code;
        Generator.addCode(`cv2.imwrite("./"+${name}, img_src)
`)
	}

    //% block="摄像并框取人脸(需在循环中执行)" blockType="command"
    export function readcaptureandangle(parameter: any, block: any) {
        Generator.addCode(`cv2.waitKey(5)
cvimg_success, img_src = cap.read()
cvimg_h, cvimg_w, cvimg_c = img_src.shape
cvimg_w1 = cvimg_h*WIDTH//HEIGHT
cvimg_x1 = (cvimg_w-cvimg_w1)//2
img_src = img_src[:, cvimg_x1:cvimg_x1+cvimg_w1]
img_src = cv2.resize(img_src, (WIDTH, HEIGHT))
gray = cv2.cvtColor(img_src, cv2.COLOR_BGR2GRAY)
face_cas = cv2.CascadeClassifier("/usr/local/lib/python3.7/dist-packages/cv2/data/haarcascade_frontalface_default.xml")
face_cas.load("/usr/local/lib/python3.7/dist-packages/cv2/data/haarcascade_frontalface_default.xml")
faceRects = face_cas.detectMultiScale(gray, scaleFactor=1.2, minNeighbors=3, minSize=(32, 32))
for faceRect in faceRects:
	x, y, w, h = faceRect
	cv2.rectangle(img_src, (x, y), (x + h, y + w), (0, 255, 0), 3)
cvimg_src=img_src.copy()
cv2.imshow('cvwindow', cvimg_src)
`)
    }

    //% block="摄像并框取人脸,写上文字[NAME](需在循环中执行)" blockType="command"
    //% NAME.shadow="string" NAME.def1="face"
    export function readcaptureandangle(parameter: any, block: any) {
        let name=parameter.NAME.code;
        Generator.addCode(`cv2.waitKey(5)
cvimg_success, img_src = cap.read()
cvimg_h, cvimg_w, cvimg_c = img_src.shape
cvimg_w1 = cvimg_h*WIDTH//HEIGHT
cvimg_x1 = (cvimg_w-cvimg_w1)//2
img_src = img_src[:, cvimg_x1:cvimg_x1+cvimg_w1]
img_src = cv2.resize(img_src, (WIDTH, HEIGHT))
gray = cv2.cvtColor(img_src, cv2.COLOR_BGR2GRAY)
face_cas = cv2.CascadeClassifier("/usr/local/lib/python3.7/dist-packages/cv2/data/haarcascade_frontalface_default.xml")
face_cas.load("/usr/local/lib/python3.7/dist-packages/cv2/data/haarcascade_frontalface_default.xml")
faceRects = face_cas.detectMultiScale(gray, scaleFactor=1.2, minNeighbors=3, minSize=(32, 32))
for faceRect in faceRects:
    x, y, w, h = faceRect
    cv2.rectangle(img_src, (x, y), (x + h, y + w), (0, 255, 0), 3)
    cv2.putText(img_src, ${name}, (x, y+w), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (0, 0, 255), 2)
cvimg_src=img_src.copy()
cv2.imshow('cvwindow', cvimg_src)
`)
    }

    //% block="摄像中，当按下[BUTTON]拍照并保存为[NAME]" blockType="command"
	//% NAME.shadow="string" NAME.defl="face.png"
	//% BUTTON.shadow="dropdown" BUTTON.options="BTN" BUTTON.defl="BTN.a"
    export function takephoto(parameter: any, block: any) {
        let name=parameter.NAME.code;
		let button=parameter.BUTTON.code;
        Generator.addCode(`while True:
	cv2.waitKey(5)
	cvimg_success, img_src = cap.read()
	cvimg_h, cvimg_w, cvimg_c = img_src.shape
	cvimg_w1 = cvimg_h*WIDTH//HEIGHT
	cvimg_x1 = (cvimg_w-cvimg_w1)//2
	img_src = img_src[:, cvimg_x1:cvimg_x1+cvimg_w1]
	img_src = cv2.resize(img_src, (WIDTH, HEIGHT))
	cvimg_src=img_src.copy()
	cv2.imshow('cvwindow', cvimg_src)
	if cv2.waitKey(1) & 0xFF == ord('${button}'):
		print("xxxxx")
		cv2.imwrite("./"+${name}, img_src)
		break
`)
    }

    //% block="保存摄像中的一帧图片为[NAME]" blockType="command"
    //% NAME.shadow="string" NAME.defl="face.png"
    export function takephotoinvideo(parameter: any, block: any) {
        let name=parameter.NAME.code;
        Generator.addCode(`
cv2.imwrite("./"+${name}, img_src)
`)
    }

    //% block="关闭摄像头" blockType="command"
    export function closecap(parameter: any, block: any) {
        Generator.addCode(`cap.release()
cv2.destroyAllWindows() 
`)
    }

    //% block="中文转拼音，中文为[NAME]" blockType="reporter"
	//% NAME.shadow="string" NAME.defl="你好"
    export function takephoto(parameter: any, block: any) {
        let name=parameter.NAME.code;
        Generator.addImport(`\nfrom c2p import chinese2pinyin`)
        Generator.addCode(`chinese2pinyin(${name})
`)
    }

    //% block="识别图片[NAME]中二维码的信息(内测中...)" blockType="reporter"
	//% NAME.shadow="string" NAME.defl="img.png"
    export function takephot2(parameter: any, block: any) {
        let name=parameter.NAME.code;
        Generator.addImport(`\nfrom pyzbar_data import get_barcode_data`)
        Generator.addCode(`get_barcode_data(${name})
`)
    }


}
